package com.tuo51.childcare.controller;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.tuo51.foudation.model.StatusResponse;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/papi/jsapi/{appid}")
@Api(value = "/papi/jsapi/{appid}", tags = "家长服务号 - JS Runtime支持")
@Slf4j
public class PAPIJsApiController {

    @Autowired
    private WxMpService wxService;

    @Autowired
    private WxPayConfig mpPayConfig;

    @GetMapping("/config")
    public StatusResponse getJsapiTicket(@RequestHeader("ticket") String ticket,
                                         @PathVariable String appid,
                                         String url) throws WxErrorException {

//        appId: '', // 必填，公众号的唯一标识
//        timestamp: , // 必填，生成签名的时间戳
//        nonceStr: '', // 必填，生成签名的随机串
//        signature: '',// 必填，签名
//        jsApiList: [] // 必填，需要使用的JS接口列表

        WxMpService mpService = this.wxService.switchoverTo(appid);
        final WxJsapiSignature jsapiSignature = mpService.createJsapiSignature(url);
        log.info("got signature for ticket ::: " + mpService.getJsapiTicket());
        log.info("" + jsapiSignature);

//        System.out.println(jsapiSignature);
//        return this.wxService.getJsapiTicket(true);
        return StatusResponse.success(jsapiSignature);
    }
}